package com.qf.ex;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 异常处理器：监听前端控制器 ，捕获前端控制器抛出的所有异常
 * @ControllerAdvice:控制器通知对象，当控制器出现异常时，通知此对象
 * @ExceptionHandler:标记再方法上，指定该方法要处理的异常
 * @RestControllerAdvice 相当于 @ControllerAdvice + @ResponseBody
 * @author YDW
 * projectName:project2202_3
 * time:2022/5/12 16:51
 * description:
 */
@ControllerAdvice
public class MyHandlerException{

    //处理自定义异常（预料到的异常）
    @ExceptionHandler({
            CustomException.class
    })
    public ModelAndView handlerCustomException(CustomException e){
        ModelAndView mav=new ModelAndView();
        mav.addObject("msg","自定义异常");
        mav.setViewName("/error.jsp");
        return mav;
    }

    //处理其他异常
    @ExceptionHandler({
            Exception.class
    })
    public ModelAndView handlerOtherException(Exception e){
        ModelAndView mav=new ModelAndView();
        mav.addObject("msg","其他异常");
        mav.setViewName("/error.jsp");
        return mav;
    }




}
